import store from '@/store';
import { listCompany, getCompanyRY } from '@/api/business/company';
import { CompanyVO, CompanyQuery, CompanyForm } from '@/api/business/company/types';

export const useCompanyStore = defineStore('useCompany', () => {
  const initFormData: CompanyForm = {
    companyId: undefined,
    companyName: undefined,
    remark: undefined
  };
  const data = reactive<PageData<CompanyForm, CompanyQuery>>({
    form: { ...initFormData },
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      companyId: undefined,
      companyName: undefined,
      remark: undefined,
      params: {}
    },
    rules: {
      companyId: [{ required: true, message: '公司id不能为空', trigger: 'blur' }],
      companyName: [{ required: true, message: '公司名称不能为空', trigger: 'blur' }]
    }
  });
  const companyList = ref<CompanyVO[]>([]);
  const companyId = ref(undefined);
  const total = ref(0);
  const { queryParams, form, rules } = toRefs(data);
  /** 查询公司列表 */
  const getList = async () => {
    const res = await listCompany(queryParams.value);
    companyList.value = res.rows;
    total.value = res.total;
    getCompanyRYList();
  };

  const companyRYList = ref([]);
  /** 查询公司人员列表 */
  const getCompanyRYList = async () => {
    const res = await getCompanyRY('');
    companyRYList.value = res;
  };

  return {
    companyId,
    companyList,
    companyRYList,
    getList,
    getCompanyRYList
  };
});

export default useCompanyStore;
// 非setup
export function useCompanyStoreHook() {
  return useCompanyStore(store);
}
